<template>
  <el-dialog width="500" v-model="dialogVisible" :title="isEdit ? '编辑行业' : '添加行业'">
    <el-form :model="form" label-width="auto">
      <el-form-item label="行业名称">
        <el-input placeholder="请输入" v-model="form.name"></el-input>
      </el-form-item>
      <el-form-item label="行业描述">
        <el-input
          type="textarea"
          :rows="4"
          placeholder="请输入"
          v-model="form.description"
        ></el-input>
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="closeDialog">取消</el-button>
      <el-button type="primary" @click="onSubmit">确定</el-button>
    </template>
  </el-dialog>
</template>

<script setup>
import { ref } from 'vue'
import { createIndustry, updateIndustry } from '@/api/enterprise'
import { ElMessage } from 'element-plus'
const form = ref({
  id: '',
  name: '',
  description: '',
})
const isEdit = ref(false)
const dialogVisible = ref(false)
const emit = defineEmits('submit')
const openDialog = (data = null) => {
  if (data) {
    console.log(data)
    isEdit.value = true
    form.value.id = data.id
    form.value.name = data.name
    form.value.description = data.description
  } else {
    isEdit.value = false
    form.value = {
      name: '',
      description: '',
    }
  }
  dialogVisible.value = true
}

// 关闭弹窗方法
const closeDialog = () => {
  dialogVisible.value = false
}
const onSubmit = async () => {
  let res
  if (isEdit.value) {
    res = await updateIndustry(form.value)
  } else {
    res = await createIndustry(form.value)
  }

  if (res.status === 200 || res.status === 201) {
    ElMessage.success(isEdit.value ? '编辑成功' : '添加成功')
    emit('submit')
    closeDialog()
  } else {
    ElMessage.error(res.data.data || '系统繁忙，请稍后再试')
  }
}
defineExpose({ openDialog, closeDialog })
</script>

<style>
</style>
